<template>
	<view class="main">
		
		<view>
			<view class="title">
				<view>方案一</view>
				<view class="num" @click="vote(first.id,1)"><uni-icons type="hand-up" size="28"></uni-icons>{{first.count}}</view>
			</view>
			<view class="row head">
				<view class="item date">日期</view>
				<view class="item">早餐</view>
				<view class="item">午餐</view>
				<view class="item">晚餐</view>
			</view>
			<block v-for="(item,index) in week">
				<view class="row">
					<view class="item date">
						{{item.weekName}}
						<view>({{item.date}})</view>
					</view>
					<view class="item">{{item.first.morning.content}}</view>
					<view class="item">{{item.first.noon.content}}</view>
					<view class="item">{{item.first.evening.content}}</view>
				</view>
			</block>
		</view>
		<view class="second">
			<view class="title">
				<view>方案二</view>
				<view class="num" @click="vote(second.id,2)"><uni-icons type="hand-up" size="28"></uni-icons>{{second.count}}</view>
			</view>
			<view class="row head">
				<view class="item date">日期</view>
				<view class="item">早餐</view>
				<view class="item">午餐</view>
				<view class="item">晚餐</view>
			</view>
			<block v-for="(item,index) in week">
				<view class="row">
					<view class="item date">
						{{item.weekName}}
						<view>({{item.date}})</view>
					</view>
					<view class="item">{{item.second.morning.content}}</view>
					<view class="item">{{item.second.noon.content}}</view>
					<view class="item">{{item.second.evening.content}}</view>
				</view>
			</block>
		</view>
	</view>
</template>

<script>
	import {
		voteList,vote
	} from '@/api/vote.js';
	export default {
		components: {
			
		},
		data() {
			return {
				list: [],
				week:[],
				activity:{},
				first:{},
				second:{}
			}
		},
		onLoad() {
			this.getWeek();
			// #ifdef MP-WEIXIN
			//小程序分享到朋友圈
			wx.showShareMenu({
				withShareTicket: true,
				menus: ['shareAppMessage', 'shareTimeline'],
			})
			// #endif
		},
		onReady() {
			
		},
		onPullDownRefresh() {

		},
		methods: {
			//：投票
			vote(option_id,type) {
				var args = {
					vote_id:this.activity.id,
					option_id:option_id
				}
				var that = this;
				this.$http.post(`${vote}`, {
						...args
					})
					.then(async data => {
						if(data.code){
							if(type == 1){
								that.first.count++;
							}
							if(type == 2){
								that.second.count++;
							}
							uni.showToast({
								title:'投票成功'
							})
						}
					})
			},
			//：
			getWeek() {
				let now = new Date(); // 当前日期
				let nowDayOfWeek = now.getDay(); // 今天本周的第几天
				let day = nowDayOfWeek || 7;
				let nowDay = now.getDate(); // 当前日
				let nowMonth = now.getMonth(); // 当前月
				var start =  Date.parse(new Date(now.getFullYear(), nowMonth, nowDay + 1 - day));//本周起始时间戳
				if(this.current == 2){
					start = start + 7*24*3600*1000;//下周起始时间戳
				}
				console.log(start);
				var oneDay = 24 * 3600 * 1000;
				for(var i = 0; i < 5;i++){
					this.formatDate(new Date(start+i*oneDay));
				}
				this.getList();
			},
			// 日期格式化
			formatDate(date) {
				let myyear = date.getFullYear();
				
				let mymonth = date.getMonth() + 1;
				let myweekday = date.getDate();
				if (mymonth < 10) {
					mymonth = '0' + String(mymonth);
				}
				if (myweekday < 10) {
					myweekday = '0' + String(myweekday);
				}
				var weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];  
				var myDate = new Date(Date.parse(date));  
				var weekName =  weekDay[myDate.getDay()]
				this.week.push({'date':mymonth + '-' + myweekday,'weekName':weekName,'first':{},second:{}});
			},
			getList:function(){
				var args = {}
				var that = this;
				this.$http.get(`${voteList}`, {
						...args
					})
					.then(async data => {
						if (data.code == 200) {
							var res = data.data[0];
							that.activity = res;
							var first = '';
							var second = '';
							res.options.forEach(function(option,index){
								if(option.project == 0){
									first = option.menu
									that.first = option;
								}
								if(option.project == 1){
									second = option.menu
									that.second = option;
								}
							})
							
							
							first.forEach(function(log,index){
								var date = new Date(log.date*1000); //获取一个时间对象
								var month = date.getMonth()+1;  // 获取月份(0-11,0代表1月,用的时候记得加上1)
								if(month<10){
									month = 0+String(month);
								}
								var day = date.getDate();  // 获取日(1-31)
								if(day<10){
									day = 0+String(day);
								}
								var mydate = month+'-'+day;
								
								that.week.forEach(function(item,index){

									if(item.date == mydate){
										
										if(log.type == 0){
											item.first.morning = log;
											that.$set(that.week,index,item);
											
										}
										if(log.type == 1){
											item.first.noon = log;
											that.$set(that.week,index,item);
										}
										if(log.type == 2){
											item.first.evening = log;
											that.$set(that.week,index,item);
										}
									}
								})
							})
							second.forEach(function(log,index){
								var date = new Date(log.date*1000); //获取一个时间对象
								var month = date.getMonth()+1;  // 获取月份(0-11,0代表1月,用的时候记得加上1)
								if(month<10){
									month = 0+String(month);
								}
								var day = date.getDate();  // 获取日(1-31)
								if(day<10){
									day = 0+String(day);
								}
								var mydate = month+'-'+day;
								
								that.week.forEach(function(item,index){
							
									if(item.date == mydate){
										
										if(log.type == 0){
											item.second.morning = log;
											that.$set(that.week,index,item);
											
										}
										if(log.type == 1){
											item.second.noon = log;
											that.$set(that.week,index,item);
										}
										if(log.type == 2){
											item.second.evening = log;
											that.$set(that.week,index,item);
										}
									}
								})
							})
						}
					});
			}
		}
	}
</script>

<style lang="scss">
	.main{background-color: #fff;padding: 30rpx 3%;}
	.row{
		display: flex;border-bottom:1px solid #ececec;
		.item{flex : 1;border-right: 1px solid #ececec;text-align: center;vertical-align: middle;}
		.date{border-left:1px solid #ececec;}
		
	}
	
	.head{
		border-top:1px solid #ececec;height: 90rpx; line-height: 90rpx;
		.item{line-height: 90rpx;}
	}
	.second{margin-top: 50rpx;}
	.title{display: flex;justify-content: space-between;align-items: center;}
</style>